import java.util.ArrayList;
import java.util.Random;
import java.net.URLEncoder;
import java.net.URL;
import java.io.File;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.FileReader;

public class Attack {
    
    private String matchTerm;
    private ArrayList<String> distractors;
    private static int NUM_TERMS = 6;
    
    // If the wiki page contains this then problem
    private static String ERROR= "Wikipedia does not have an article with this exact name";

    public Attack(File page){
    	String pageText = getContents(page);
    	this.distractors = new ArrayList<String>();
    	fileGetMatchTerm(pageText);
    	getPossibleAnswers(pageText);
    	
    }
    
    
    // Provide the results
    public static void main (String[] args){
       	File f = new File("Tests/");
       	File[] fileList = f.listFiles();
    	Attack a;
    	for(File file : fileList){
    	    if(file.isFile()){
    		System.out.println("name is: "+file.getName());
    		a = new Attack(file);
    		
    		System.out.println(a.getMatchTerm());
    		ArrayList<String> d = a.performAttack();
    		for(String s : d){
    			System.out.println(s);
    		}
    		System.out.println("--------------");
    	   }
    	}
    }
    
    private void fileGetMatchTerm(String page){
    	//The term to match is:     Bird</h3>
    	int index = 0;
    	index = page.indexOf("The term to match is:", index) + 21;
    	this.matchTerm = page.substring(index, page.indexOf("</h3>", index)).trim();
    }
    
    public void getPossibleAnswers(String page){
    	int index = 0;
    	String distract;
    	// Not the best code if the number changes
   	for(int counter =0;counter < 6;counter++){
	   index = page.indexOf("alt='", index) + 5;
    	   distract = page.substring(index, page.indexOf("'", index)).trim();
   	   this.distractors.add(distract);
   	}
    }
    
  static public String getContents(File aFile) {
    StringBuilder contents = new StringBuilder();
    try {
      BufferedReader input =  new BufferedReader(new FileReader(aFile));
      try {
        String line = null; //not declared within while loop
        while (( line = input.readLine()) != null){
          contents.append(line);
          contents.append(System.getProperty("line.separator"));
        }
      }
      finally {
        input.close();
      }
    }
    catch (IOException ex){
      ex.printStackTrace();
    }
    
    return contents.toString();
  }
  
  public ArrayList<String> performAttack(){
  ArrayList<String> possibleResults = null;
  String page = null;
  try{
  	possibleResults = new ArrayList<String>();
  	String t = this.matchTerm.replace(' ','_');
  	page = new HTTPPage("http://en.wikipedia.org/wiki/"+t).getPage();
  	//System.out.println(page);
  	
  	if(page.contains(Attack.ERROR))
  		return null;
  	for(String term : this.distractors){
  		if(page.contains(term)){
  			possibleResults.add(term);
  		}
  	}
  
  }catch(Exception e){
  	System.out.println("There was an error in the process");
  	e.printStackTrace();
  	return null;
  }
  	return possibleResults;
  }
  
  
  public String getMatchTerm(){
  	return this.matchTerm;
  }

}
